package org.guajava.controller.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.guajava.common.annotation.Log;
import org.guajava.common.base.AjaxResult;
import org.guajava.common.enums.BusinessType;
import org.guajava.domain.SysConfigInfo;
import org.guajava.framework.util.ShiroUtils;
import org.guajava.framework.web.base.BaseController;
import org.guajava.framework.web.page.TableDataInfo;
import org.guajava.service.ISysConfigService;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/system/config")
public class SysConfigController extends BaseController {

    private String prefix = "system/config";

    @Autowired
    private ISysConfigService configService;

    /**
     * 加载页面
     */
    @GetMapping()
    public String post() {
        return prefix + "/config";
    }

    /**
     * 加载table
     */
    @PostMapping("/configList")
    @ResponseBody
    public TableDataInfo list(SysConfigInfo sysConfigInfo)
    {
        startPage();
        List<SysConfigInfo> list = configService.selectConfigList(sysConfigInfo);
        return getDataTable(list);
    }
    /**
     * 编辑参数信息页面
     */
    @RequestMapping("/editForm")
    public String addPost(Model model) {
        return "system/config/configForm.html";
    }

    /**
     * 新增参数信息
     * @param sysConfigInfo
     */
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysConfigInfo sysConfigInfo)
    {
        sysConfigInfo.setCreateBy(ShiroUtils.getLoginName());
        sysConfigInfo.setCreateTime(new Date());
        return toAjax(configService.insertSelective(sysConfigInfo));
    }

    /**
     * 删除参数信息
     * @param ids
     * @return
     */
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(configService.deleteByPrimaryKey(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 修改参数信息
     * @param sysConfigInfo
     * @return
     */
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysConfigInfo sysConfigInfo)
    {
        sysConfigInfo.setUpdateBy(ShiroUtils.getLoginName());
        sysConfigInfo.setUpdateTime(new Date());
        return toAjax(configService.updateByPrimaryKeySelective(sysConfigInfo));
    }
}
